package com.argrace.smart.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.aliyun.alink.business.helper.ChannelBindHelper;
import com.aliyun.alink.business.login.AlinkLoginBusiness;
import com.aliyun.alink.business.login.IAlinkLoginCallback;
import com.argrace.smart.R;
import com.argrace.smart.activity.CheckOutActivity;
import com.argrace.smart.activity.MyRoomDeviceListActivity;
import com.argrace.smart.activity.RuoQi_DeviceListActivity;
import com.argrace.smart.base.BaseFragment;
import com.argrace.smart.base.ConstantConfigure;
import com.argrace.smart.model.LocalDb_AnJianTable;
import com.argrace.smart.model.LocalDb_DeviceNameAndRoomNameTable;
import com.argrace.smart.model.LocalDb_DeviceSyncTable;
import com.argrace.smart.model.LocalDb_QingJingTable;
import com.argrace.smart.model.LocalDb_QingJing_DeviceTable;
import com.argrace.smart.model.LocalDb_RelationDeviceTable;
import com.argrace.smart.model.LocalDb_RoomNameTable;
import com.argrace.smart.model.LocalDb_RuoQiDeviceTable;
import com.argrace.smart.model.LocalDb_ShuangKongFuSheBeiTable;
import com.argrace.smart.util.XmlUtils;

import org.litepal.crud.DataSupport;

/**
 * Created by argrace_dev_1 on 2018/2/8.
 */

public class MineFragment extends BaseFragment {
    private TextView userid;
    private RelativeLayout logout;
    private RelativeLayout ruoqi;
    private RelativeLayout mydevice;
    private RelativeLayout clean;
    private RelativeLayout checkout;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_mine, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        init();
        super.onActivityCreated(savedInstanceState);
    }

    private void init() {
        setView();

    }

    private void setView() {
        userid = (TextView) getView().findViewById(R.id.fragment_mine_userid);
        logout = (RelativeLayout) getView().findViewById(R.id.fragment_mine_logout);
        ruoqi = (RelativeLayout) getView().findViewById(R.id.fragment_mine_ruoqi);
        mydevice = (RelativeLayout) getView().findViewById(R.id.fragment_mine_mydevice);
        clean = (RelativeLayout) getView().findViewById(R.id.fragment_mine_clean);
        checkout = (RelativeLayout) getView().findViewById(R.id.fragment_mine_checkout);
        checkout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), CheckOutActivity.class);
                startActivity(intent);
            }
        });
        userid.setText(getActivity().getSharedPreferences(ConstantConfigure.SPLOGIN_NAME, getActivity().MODE_PRIVATE).getString("mobile", "000"));
        logout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlinkLoginBusiness.getInstance().logout(getActivity(), new IAlinkLoginCallback() {
                    @Override
                    public void onSuccess() {
                        toastActBtm("登出成功");
                        getActivity().finish();
                        AlinkLoginBusiness.getInstance().logout(getActivity(), null);
                    }

                    @Override
                    public void onFailure(int i, String s) {
                        toastActBtm("登出失败" + s);
                    }
                });
            }
        });
        ruoqi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), RuoQi_DeviceListActivity.class);
                startActivity(intent);
            }
        });
        mydevice.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), MyRoomDeviceListActivity.class);
                startActivity(intent);
            }
        });
        clean.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DataSupport.deleteAll(LocalDb_RelationDeviceTable.class);
                DataSupport.deleteAll(LocalDb_AnJianTable.class);
                DataSupport.deleteAll(LocalDb_QingJingTable.class);
                DataSupport.deleteAll(LocalDb_QingJing_DeviceTable.class);
                DataSupport.deleteAll(LocalDb_RoomNameTable.class);
                DataSupport.deleteAll(LocalDb_DeviceNameAndRoomNameTable.class);
                DataSupport.deleteAll(LocalDb_DeviceSyncTable.class);
                DataSupport.deleteAll(LocalDb_ShuangKongFuSheBeiTable.class);
                DataSupport.deleteAll(LocalDb_RuoQiDeviceTable.class);
//                XmlUtils.savexml();
            }
        });
    }


}
